package vn.outsource.weather.ui;

import java.util.ArrayList;
import java.util.Arrays;

import vn.tdx.androidlibs.ui.BaseFragmentActivity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.example.com.outsource.weather.R;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;

public class LocationActivity extends BaseFragmentActivity implements OnClickListener{

	private ImageView mimgLeftNavigation;
	private ImageView mimgRighNavigation;
	
	private ArrayAdapter<String> adapter;
	public int removeMode = DragSortController.FLING_REMOVE;

	private String[] array = { "Ha Noi", "Hai PHong", "Hai Duong", "Hue",
			"Da Nang"};
	private ArrayList<String> list;

	private DragSortListView mDragSortListView;
	private DragSortController mController;

	private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			if (from != to) {
				String item = adapter.getItem(from);
				adapter.remove(item);
				adapter.insert(item, to);
			}
		}
	};

	@Override
	protected void setContentView(Bundle bundle) {
		setContentView(R.layout.activity_location);
	}
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		initView();initData();
	}
	private void initView(){
		mimgLeftNavigation=(ImageView)findViewById(R.id.cv_navigation_bar_img_left);
		mimgRighNavigation=(ImageView)findViewById(R.id.cv_navigation_bar_img_right);
		mimgRighNavigation.setImageResource(R.drawable.btn_add_highlight);
		mimgLeftNavigation.setImageResource(R.drawable.btn_back);
		mimgLeftNavigation.setOnClickListener(this);
		mimgRighNavigation.setOnClickListener(this);
		
		mDragSortListView = (DragSortListView) findViewById(R.id.activity_location_list);
		mController = buildController(mDragSortListView);
		mDragSortListView.setFloatViewManager(mController);
		mDragSortListView.setOnTouchListener(mController);
		mDragSortListView.setDragEnabled(true);
		mDragSortListView.setDropListener(onDrop);
		
		mDragSortListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				AlertDialog.Builder builder=new Builder(LocationActivity.this);
				builder.setTitle(R.string.confirm);
				builder.setMessage(R.string.are_you_sure_);
				builder.setNegativeButton(R.string.no, null);
				builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						adapter.remove(list.get(arg2));
//						list.remove(arg2);
						adapter.notifyDataSetChanged();
					}
				});
				builder.show();
				
			}
		});
	}
	private void initData(){
		list = new ArrayList<String>(Arrays.asList(array));

		// locationAdapter=new LocationAdapter(getApplicationContext(), array);
		adapter = new ArrayAdapter<String>(this, getItemLayout(), R.id.text,
				list);
		mDragSortListView.setAdapter(adapter);
	}

	/**
	 * Return list item layout resource passed to the ArrayAdapter.
	 */
	protected int getItemLayout() {
		/*
		 * if (removeMode == DragSortController.FLING_LEFT_REMOVE || removeMode
		 * == DragSortController.SLIDE_LEFT_REMOVE) { return
		 * R.layout.list_item_handle_right; } else
		 */
		if (removeMode == DragSortController.CLICK_REMOVE) {
			return R.layout.list_item_click_remove;
		} else {
			return R.layout.list_item_handle_left;
		}
	}

	/**
	 * Called in onCreateView. Override this to provide a custom
	 * DragSortController.
	 */
	public DragSortController buildController(DragSortListView dslv) {
		// defaults are
		// dragStartMode = onDown
		// removeMode = flingRight
		DragSortController controller = new DragSortController(dslv);
		controller.setDragHandleId(R.id.drag_handle);
		controller.setClickRemoveId(R.id.click_remove);
		controller.setRemoveEnabled(true);
		controller.setSortEnabled(true);
		controller.setDragInitMode(DragSortController.ON_DOWN);
		// controller.setRemoveMode(removeMode);
		return controller;
	}


	@Override
	protected boolean isNavigationEnable() {
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cv_navigation_bar_img_left:
			finish();
			break;
		case R.id.cv_navigation_bar_img_right:
			startActivity(new Intent(getApplicationContext(), AddLocationActivity.class));
			break;

		default:
			break;
		}
	}

}
